package com.fate.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.fate.framework.utils.SystemUtil;
import com.fate.system.entity.User;
import com.fate.system.service.*;
import com.fate.system.vo.baseinfo.BaseInfoVO;
import com.fate.system.vo.menu.MenuTreeVO;
import com.fate.system.vo.user.UserDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户基本信息 API")
@RestController
@RequestMapping("/api/v.10/main")
public class MainContainer {

  @Resource private IUserService userService;
  @Resource private IMenuService menuService;
  @Resource private IRoleMenuService roleMenuService;
  @Resource private IDictService dictService;
  @Resource private IRoleService roleService;

  @Operation(summary = "获取用户总体信息")
  @GetMapping("/totalBaseInfo")
  public BaseInfoVO totalBaseInfo() {
    String loginId = (String) StpUtil.getLoginId();
    User user = userService.getById(loginId);
    UserDetailVO userDetailVO = SystemUtil.buildResource(user, UserDetailVO.class);
    // 翻译字典
    dictService.transfer(userDetailVO);

    // 获取用户角色ids
    List<String> roleIds = user.getRoleIds();

    // 用户角色翻译
    List<String> roleNames = roleService.getRoleNameByIds(roleIds);
    userDetailVO.setRoleNames(roleNames);

    // 获取角色菜单id列表
    List<String> menuIds = roleMenuService.getMenuIdsByRoleIds(roleIds);
    List<MenuTreeVO> menuTree = menuService.getMenuTree(menuIds);

    // 菜单字典翻译
    dictService.transfer(menuTree);

    return new BaseInfoVO(userDetailVO, menuTree);
  }
}
